/**
 * Generate code from /{{projectName}}-core/src/main/java/{{packageName}}/{{apis}}/rest/Abstract{{apiEntities}}Resource.java.hbs
 */
package cn.ibizlab.api.rest;

import java.util.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.HttpStatus;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.util.ObjectUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.ApiOperation;
import cn.ibizlab.util.domain.ImportResult;
import cn.ibizlab.api.dto.*;
import cn.ibizlab.api.mapping.*;
import cn.ibizlab.core.ad.domain.SysActiveDirectory;
import cn.ibizlab.core.ad.service.SysActiveDirectoryService;
import cn.ibizlab.core.ad.filter.SysActiveDirectorySearchContext;

/**
 * 实体[SysActiveDirectory] rest实现
 *
 * @author generator
 */
@Slf4j
public abstract class AbstractSysActiveDirectoryResource {

    @Autowired
    public SysActiveDirectoryService sysActiveDirectoryService;

    @Autowired
    @Lazy
    public SysActiveDirectoryDTOMapping sysActiveDirectoryDtoMapping;

    @Autowired
    @Lazy
    public SysActiveDirectoryFilterDTOMapping sysActiveDirectoryFilterDtoMapping;


    /**
    * 创建Create AD
    * 
    *
    * @param dto dto
    * @return ResponseEntity<SysActiveDirectoryDTO>
    */
    @ApiOperation(value = "创建Create", tags = {"AD" },  notes = "SysActiveDirectory-Create ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysActiveDirectory-Create-all')")
    @PostMapping(value = {"sys_active_directories","sys_active_directories"})
    public ResponseEntity<SysActiveDirectoryDTO> create
            (@Validated @RequestBody SysActiveDirectoryDTO dto) {
        SysActiveDirectory domain = sysActiveDirectoryDtoMapping.toDomain(dto);
        sysActiveDirectoryService.create(domain);
        SysActiveDirectory rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(sysActiveDirectoryDtoMapping.toDto(rt));
    }

    /**
    * 获取Get AD
    * 
    *
    * @param id id
    * @return ResponseEntity<SysActiveDirectoryDTO>
    */
    @ApiOperation(value = "获取Get", tags = {"AD" },  notes = "SysActiveDirectory-Get ")
    @GetMapping("sys_active_directories/{id}")
    public ResponseEntity<SysActiveDirectoryDTO> getById
            (@PathVariable("id") String id) {
        SysActiveDirectory rt = sysActiveDirectoryService.get(id);
        return ResponseEntity.status(HttpStatus.OK).body(sysActiveDirectoryDtoMapping.toDto(rt));
    }

    /**
    * 删除Remove AD
    * 
    *
    * @param id id
    * @return ResponseEntity<Boolean>
    */
    @ApiOperation(value = "删除Remove", tags = {"AD" },  notes = "SysActiveDirectory-Remove ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysActiveDirectory-Remove-all')")
    @DeleteMapping("sys_active_directories/{id}")
    public ResponseEntity<Boolean> removeById
            (@PathVariable("id") String id) {
        Boolean rt = sysActiveDirectoryService.remove(id);
        return ResponseEntity.status(HttpStatus.OK).body(rt);
    }

    /**
    * 更新Update AD
    * 
    *
    * @param id id
    * @param dto dto
    * @return ResponseEntity<SysActiveDirectoryDTO>
    */
    @ApiOperation(value = "更新Update", tags = {"AD" },  notes = "SysActiveDirectory-Update ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysActiveDirectory-Update-all')")
    @PutMapping("sys_active_directories/{id}")
    public ResponseEntity<SysActiveDirectoryDTO> updateById
            (@PathVariable("id") String id, @Validated @RequestBody SysActiveDirectoryDTO dto) {
        SysActiveDirectory domain = sysActiveDirectoryDtoMapping.toDomain(dto);
        domain.setId(id);
        sysActiveDirectoryService.update(domain);
        SysActiveDirectory rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(sysActiveDirectoryDtoMapping.toDto(rt));
    }

    /**
    * 校验CheckKey AD
    * 
    *
    * @param dto dto
    * @return ResponseEntity<Integer>
    */
    @ApiOperation(value = "校验CheckKey", tags = {"AD" },  notes = "SysActiveDirectory-CheckKey ")
    @PostMapping("sys_active_directories/checkkey")
    public ResponseEntity<Integer> checkKey
            (@Validated @RequestBody SysActiveDirectoryDTO dto) {
        SysActiveDirectory domain = sysActiveDirectoryDtoMapping.toDomain(dto);
        Integer rt = sysActiveDirectoryService.checkKey(domain);
        return ResponseEntity.status(HttpStatus.OK).body(rt);
    }

    /**
    * 草稿GetDraft AD
    * 
    *
    * @param dto dto
    * @return ResponseEntity<SysActiveDirectoryDTO>
    */
    @ApiOperation(value = "草稿GetDraft", tags = {"AD" },  notes = "SysActiveDirectory-GetDraft ")
    @GetMapping("sys_active_directories/getdraft")
    public ResponseEntity<SysActiveDirectoryDTO> getDraft
            (@SpringQueryMap SysActiveDirectoryDTO dto) {
        SysActiveDirectory domain = sysActiveDirectoryDtoMapping.toDomain(dto);
        SysActiveDirectory rt = sysActiveDirectoryService.getDraft(domain);
        return ResponseEntity.status(HttpStatus.OK).body(sysActiveDirectoryDtoMapping.toDto(rt));
    }

    /**
    * Items AD
    * 根据传入的id 查询 id对应单位的AD清单，不传入id时自动取当前用户可见AD清单，其余可选过滤参数:department_id/filter/parent_id
    *
    * @param id id
    * @param dto dto
    * @return ResponseEntity<List<SysActiveDirectoryDTO>>
    */
    @ApiOperation(value = "Items", tags = {"AD" },  notes = "SysActiveDirectory-Items 根据传入的id 查询 id对应单位的AD清单，不传入id时自动取当前用户可见AD清单，其余可选过滤参数:department_id/filter/parent_id")
    @PostMapping("sys_active_directories/{id}/items")
    public ResponseEntity<List<SysActiveDirectoryDTO>> itemsById
            (@PathVariable("id") String id, @Validated @RequestBody SysActiveDirectoryDTO dto) {
        SysActiveDirectory domain = sysActiveDirectoryDtoMapping.toDomain(dto);
        domain.setId(id);
        List<SysActiveDirectory> rt = sysActiveDirectoryService.items(domain);
        return ResponseEntity.status(HttpStatus.OK).body(sysActiveDirectoryDtoMapping.toDto(rt));
    }

    /**
    * 保存Save AD
    * 
    *
    * @param dto dto
    * @return ResponseEntity<SysActiveDirectoryDTO>
    */
    @ApiOperation(value = "保存Save", tags = {"AD" },  notes = "SysActiveDirectory-Save ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysActiveDirectory-Save-all')")
    @PostMapping("sys_active_directories/save")
    public ResponseEntity<SysActiveDirectoryDTO> save
            (@Validated @RequestBody SysActiveDirectoryDTO dto) {
        SysActiveDirectory domain = sysActiveDirectoryDtoMapping.toDomain(dto);
        sysActiveDirectoryService.save(domain);
        SysActiveDirectory rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(sysActiveDirectoryDtoMapping.toDto(rt));
    }

    /**
    * 查询FetchDefault AD
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<SysActiveDirectoryDTO>>
    */
    @ApiOperation(value = "查询FetchDefault", tags = {"AD" },  notes = "SysActiveDirectory-FetchDefault ")
    @PostMapping("sys_active_directories/fetchdefault")
    public ResponseEntity<List<SysActiveDirectoryDTO>> fetchDefault
            (@Validated @RequestBody SysActiveDirectoryFilterDTO dto) {
        SysActiveDirectorySearchContext context = sysActiveDirectoryFilterDtoMapping.toDomain(dto);
        Page<SysActiveDirectory> domains = sysActiveDirectoryService.searchDefault(context) ;
        List<SysActiveDirectoryDTO> list = sysActiveDirectoryDtoMapping.toDto(domains.getContent());
            return ResponseEntity.status(HttpStatus.OK)
            .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
            .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
            .header("x-total", String.valueOf(domains.getTotalElements()))
            .body(list);
    }

    /**
    * 查询FetchMyOrg AD
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<SysActiveDirectoryDTO>>
    */
    @ApiOperation(value = "查询FetchMyOrg", tags = {"AD" },  notes = "SysActiveDirectory-FetchMyOrg ")
    @PostMapping("sys_active_directories/fetchmyorg")
    public ResponseEntity<List<SysActiveDirectoryDTO>> fetchMyOrg
            (@Validated @RequestBody SysActiveDirectoryFilterDTO dto) {
        SysActiveDirectorySearchContext context = sysActiveDirectoryFilterDtoMapping.toDomain(dto);
        Page<SysActiveDirectory> domains = sysActiveDirectoryService.searchMyOrg(context) ;
        List<SysActiveDirectoryDTO> list = sysActiveDirectoryDtoMapping.toDto(domains.getContent());
            return ResponseEntity.status(HttpStatus.OK)
            .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
            .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
            .header("x-total", String.valueOf(domains.getTotalElements()))
            .body(list);
    }


    /**
    * 批量新建AD
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysActiveDirectory-Create-all')")
    @ApiOperation(value = "批量新建AD", tags = {"AD" },  notes = "批量新建AD")
	@PostMapping("sys_active_directories/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<SysActiveDirectoryDTO> dtos) {
        sysActiveDirectoryService.createBatch(sysActiveDirectoryDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量删除AD
    * @param ids ids
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysActiveDirectory-Remove-all')")
    @ApiOperation(value = "批量删除AD", tags = {"AD" },  notes = "批量删除AD")
	@DeleteMapping("sys_active_directories/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        sysActiveDirectoryService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量更新AD
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysActiveDirectory-Update-all')")
    @ApiOperation(value = "批量更新AD", tags = {"AD" },  notes = "批量更新AD")
	@PutMapping("sys_active_directories/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<SysActiveDirectoryDTO> dtos) {
        sysActiveDirectoryService.updateBatch(sysActiveDirectoryDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量保存AD
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysActiveDirectory-Save-all')")
    @ApiOperation(value = "批量保存AD", tags = {"AD" },  notes = "批量保存AD")
	@PostMapping("sys_active_directories/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<SysActiveDirectoryDTO> dtos) {
        sysActiveDirectoryService.saveBatch(sysActiveDirectoryDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量导入AD
    * @param config 导入模式
    * @param ignoreError 导入中忽略错误
    * @return ResponseEntity<ImportResult>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysActiveDirectory-Save-all')")
    @ApiOperation(value = "批量导入AD", tags = {"AD" },  notes = "批量导入AD")
	@PostMapping("sys_active_directories/import")
    public ResponseEntity<ImportResult> importData(@RequestParam(value = "config" , required = false) String config ,@RequestParam(value = "ignoreerror", required = false, defaultValue = "true") Boolean ignoreError ,@RequestBody List<SysActiveDirectoryDTO> dtos) {
        return  ResponseEntity.status(HttpStatus.OK).body(sysActiveDirectoryService.importData(config,ignoreError,sysActiveDirectoryDtoMapping.toDomain(dtos)));
    }

}
